<template>
  <div>
    <TopFilter :list="topFilterList" :cols="3" :collapse="false" :isSubmitBtn="false"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="fyhworkerLog"
      :isSelectColumn="false"
      :columns="columns"
      :dataSource="tableList"
      :onSyncTableData="syncTableData"
      :onColumnsChange="columns => (this.columns = columns)"
    ></FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <!-- <AjaxButton type="primary" :click="saveWorkerLog">保 存</AjaxButton> -->
      <el-button @click="closeWorkerLog">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import dictCode from '@/utils/dictCode';
import { searchRoleByCode, searchRoleByCodes } from '@/api/common/empSelect/index';
import { jsonToHump } from '@/utils/index';
import { queryRoAssignByRoNOs, saveRepairAssign } from '@/api/repair/repairAssign/repairAssign';
import { maintainWorkType } from '@/api/common/auth/employeeAuth';
import { getGongzSelect } from '@/api/repair/business/customer';

export default {
  name: 'workerLogs',
  mixins: [dictionary],
  props: ['selectColumns'],
  data() {
    return {
      topFilterList: this.createTopFilter(),
      columns: this.createTableColumns(),
      tableList: []
    };
  },

  mounted() {
    this.createBaseTable();
    this.getData();
    this.queryWorkerLog();
    this.serviceConsultant();
  },

  methods: {
    createTopFilter() {
      return [
        {
          type: 'INPUT',
          label: '项目名称',
          fieldName: 'LABOUR_NAME',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '派工工时',
          fieldName: 'ASSIGN_LABOUR_HOUR',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '检查结果',
          fieldName: 'TROUBLE_DESC',
          disabled: true
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '工位',
          dataIndex: 'LABOUR_POSITION_NAME',
          sorter: true,
          filter: true,
          editable: false,
          editType: 'select',
          editItems: []
        },
        {
          title: '责任技师',
          dataIndex: 'TECHNICIAN',
          sorter: true,
          editable: false,
          editRequired: true,
          defaultEditable: true,
          editType: 'select',
          editItems: [],
          filter: true,
          filterType: 'checkbox',
          filterItems: []
        },
        // {
        //   title: '工种',
        //   dataIndex: 'WORKER_TYPE_NAME',
        //   sorter: true,
        //   filter: true,
        //   editable: false,
        //   editType: 'select',
        //   editItems: []
        // },
        {
          title: '派工工时',
          dataIndex: 'ASSIGN_LABOUR_HOUR',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2
        },
        {
          title: '开工时间',
          dataIndex: 'ITEM_START_TIME',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          width: 150
        },
        {
          title: '完工时间',
          dataIndex: 'ITEM_END_TIME',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          width: 150
        }
      ];
    },

    createBaseTable() {
      this.BaseTable = this.$refs.table.$refs.pageTable;
    },

    // 下拉框接口数据
    async serviceConsultant() {
      let technician = { role: dictCode.TECHNICIAN }; // 指定技师
      const technicianRes = await searchRoleByCodes(technician);
      let res = technicianRes.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      // let ress = technicianRes.data.map(x => ({ text: x.employeeName, value: x.employeeNo }));
      // ress.forEach(x =>{res.push(x)});
      this.columns.find(x => x.dataIndex === 'TECHNICIAN').editItems = this.columns.find(x => x.dataIndex === 'TECHNICIAN').filterItems = res;

      // 工种
      // const workTypeRes = await getGongzSelect();
      // console.log(workTypeRes, 'workTypeRes-------');
      // if (workTypeRes.data.length) {
      //   this.columns.find(x => x.dataIndex === 'WORKER_TYPE_CODE').editItems = workTypeRes.map(x => ({ value: x.WORKER_TYPE_CODE, text: x.WORKER_TYPE_NAME }));
      // }
    },

    // 派工情况查询
    async queryWorkerLog() {
      const itemId = this.selectColumns.ITEM_ID;
      const res = await queryRoAssignByRoNOs(itemId);
      console.log(res, 'res');
      this.tableList = res.data.rows;
    },

    // syncTableFun() {},

    // 数据赋值
    getData() {
      this.topFilterList[0].initialValue = this.selectColumns.LABOUR_NAME;
      this.topFilterList[1].initialValue = this.selectColumns.ASSIGN_LABOUR_HOUR;
      this.topFilterList[2].initialValue = this.selectColumns.TROUBLE_DESC;
    },
    syncTableData(val) {
      this.tableList = val;
    },
    async saveWorkerLog() {
      console.log('GET_REQUIRED_ERROR', this.BaseTable.GET_REQUIRED_ERROR());
      if (this.BaseTable.GET_REQUIRED_ERROR() == null) {
        this.BaseTable.START_LOADING();
        try {
          let data = _.cloneDeep(this.tableList);
          for (const item of data) {
            jsonToHump(item);
          }
          // console.log(data, 'data');
          let res = await saveRepairAssign(data);
          if (res.resultCode == 200) {
            this.$notify.success({ title: '提示信息', message: '保存成功！' });
            this.tableList = [];
            this.queryWorkerLog();
          }
        } catch (error) {
        } finally {
          this.BaseTable.STOP_LOADING();
        }
      }
    },
    closeWorkerLog() {
      this.$emit('closeWorker', false);
    }
  }
};
</script>

<style lang="less"></style>
